import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


public class Sala extends GestorBD{
public String nom;
public String descripcio;
public int superficie;
public String Nomcentre;

public Sala(){

}



public Sala(String nom, String descripcio, int superficie, String nomcentre) {
	super();
	this.nom = nom;
	this.descripcio = descripcio;
	this.superficie = superficie;
	Nomcentre = nomcentre;
}



public String getNom() {
	return nom;
}
public void setNom(String nom) {
	this.nom = nom;
}
public String getDescripcio() {
	return descripcio;
}
public void setDescripcio(String descripcio) {
	this.descripcio = descripcio;
}
public int getSuperficie() {
	return superficie;
}
public void setSuperficie(int superficie) {
	this.superficie = superficie;
}

public String getNomcentre() {
	return Nomcentre;
}



public void setNomcentre(String nomcentre) {
	Nomcentre = nomcentre;
}



@Override
public String toString() {
	return nom;
}
public void grabarBD(){
	PreparedStatement pstmt = null;
	ResultSet rs =null;
	Statement stmt = null;
	
	
	try{
		stmt = this.conectar().createStatement();
	
	if (existeixBD()){
		pstmt = this.conectar().prepareStatement("UPDATE sala SET descripcio=?,superficie=?,nomCentre=? WHERE nom= '"+this.nom+"'");
		pstmt.setString(1, this.descripcio);
		pstmt.setInt(2, this.superficie);
		pstmt.setString(3, this.Nomcentre);
		pstmt.executeUpdate();
		System.out.println("S'ha modificat el registre amb ID " + this.getNom());
	}else{
		pstmt = this.conectar().prepareStatement("INSERT INTO sala(nom,descripcio,superficie,nomCentre) VALUES (?,?,?,?)");
		pstmt.setString(1, this.nom);
		pstmt.setString(2, this.descripcio);	
		pstmt.setInt(3, this.superficie);
		pstmt.setString(4, this.Nomcentre);
		pstmt.executeUpdate();
		System.out.println("S'ha inserit el registre amb ID " + this.getNom());
	}
	}
	catch(Exception e){
		e.printStackTrace();
	}
}

public void eliminarBD(){
	PreparedStatement pstmt = null;
	ResultSet rs =null;
	Statement stmt = null;
	try{
		stmt = this.conectar().createStatement();
		pstmt = this.conectar().prepareStatement("DELETE FROM sala WHERE nom='"+this.nom+"'");
		pstmt.executeUpdate();
		System.out.println("S'ha eliminat el registre amb ID " + this.getNom());
	}
	catch(Exception e){
		e.printStackTrace();
	}
}	

public boolean existeixBD(){
	// TODO Auto-generated method stub
	Statement stmt = null;
	ResultSet rs=null;
	
	try{
		stmt = connexio.createStatement();
		rs = stmt.executeQuery("select COUNT(*) from sala where nom LIKE '"+this.nom+"'");
		rs.next();
		int count = rs.getInt(1);
		if(count==0)	return false;
		else if(count==1) return true;
		else{
			System.err.println("Incoherencia en la BD, hi ha elements amb K repetida");
			return false;
		}
	}catch(Exception e){
		System.err.println("Ha saltat una excepcio");
		e.printStackTrace();
		return false;
	}finally{
		// release the connection
		try{
			if(stmt!=null)
				stmt.close();
		}catch(Exception e1){}
		
		try{
			if(connexio!=null) connexio.close();
		}catch(Exception e){}
	}
}


}
